<?php
/**
 * @file imagecollectiveaction.php
 * @author LinLin
 * @date 2016年6月1日 下午1:46:10
 * @brief 
 */
namespace haibao\cms\business\control\pc\action;

use haibao\cms\model\filter\ImageLibrary as ImageLibraryFitler;
use haibao\cms\model\filter\imagecollective\ImageCollective as ImageCollFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectiveTag as ImageCollTagFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectivePro as ImageCollProFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectiveProperty as ImageCollPropertyFilter;
use haibao\frame\data\query\Condition;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;
use \haibao\cms\business\control\pc\entity\ImageCollectiveEntity;

class ImageCollectiveAction extends BaseAction{
    const INFO_TYPE_ARTICLE = 1;//资讯类型 文章
    const INFO_TYPE_IMAGE = 2;//资讯类型 图集
    const INFO_TYPE_ARTICLE_PIC = 3;//资讯类型 文章读图模式
    
    const DATE_TYPE_WEEK = 1;// 1周
    const DATE_TYPE_MONTH = 2;// 1月
    const DATE_TYPE_HALFWEEK = 3;// 半月
    const DATE_TYPE_THREEDAY = 4;//3天之内
    const DATE_TYPE_TWO_DAYS_AGO = 5;//3天前
    
    const SORT_DESC = 'desc';
	const SORT_ASC = 'asc';
    
    public $imageCollectiveEntity;
    private static $_instance;
    
    private $pager;
    private $queryData;
    
    private $articleIdArr = array();
    private $collIdArr = array();
    
    private $tagIdArr = array();
    private $collTagIdArr = array();
    
    private $articles = array();
    private $imageColls = array();
    
    private $articleProImages = array();
    private $collProImages = array();
    
    private $tagInfo = array();
    private $collTagInfo = array();
    
    private $articleTagArr = array();//文章关联标签数组
    private $collTagArr = array();//图集关联标签数组
    
    public static function initImageCollectiveAction(){
        if(!self::$_instance instanceof self){
            self::$_instance = new self;
            self::$_instance->queryData = new \haibao\cms\data\imagecollective\ImageCollective();
        }
        self::$_instance->initVariable();
        return self::$_instance;
    }
    
    public function setImageCollectiveEntity($imageCollectiveEntity){
        $this->imageCollectiveEntity = $imageCollectiveEntity;
    }
    
    /**
     * 最近一周时间范围内，全站？级图集中，浏览量最高的前？个。 单独的一个方法4
     */
    public function getImageCollectiveByClick(){
        $notIn = $imageColls = $complateImageColls = array();
        $imageColls = $this->getImageCollForEnoughWeekData($notIn,true, $this->imageCollectiveEntity->getLimit());
        if($this->imageCollectiveEntity->getIsProImage()){
            foreach($imageColls as $coll){
                array_push($this->collIdArr,$coll->Id);
                array_push($this->collTagIdArr,$coll->ImageCollectiveTag->TagId);
                $coll->SiteType = self::INFO_TYPE_IMAGE;
            }
            if($this->collIdArr){
                $this->collProImages = $this->getCollProImage($this->collIdArr,$this->imageCollectiveEntity->getProImageLevel());
            }
        }
        $this->collTagInfo = PublicControllerBusiness::getTagInfoById($this->collTagIdArr);
        return array($imageColls,$this->collProImages,$this->collTagInfo);
    }
    
    private function getImageCollForEnoughWeekData($notIn=array(),$enough=true,$limit){
    
        $this->imageCollectiveEntity->proImageLevel = (array)$this->imageCollectiveEntity->proImageLevel;
        $proImageArr = array();
        $imageCollFilter = new ImageCollFilter();
        $imageCollFilter->select(
            array(
                ImageCollFilter::CONDITION_FIELD_NAME_ID,
                ImageCollFilter::CONDITION_FIELD_NAME_TITLE,
                ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,
                ImageCollFilter::CONDITION_FIELD_NAME_SHORT_TITLE,
                ImageCollFilter::CONDITION_FIELD_NAME_LEVEL
            )
        );
        $imageCollFilter->limit($this->imageCollectiveEntity->getOffset(),$limit);
        if($enough){
            if($this->imageCollectiveEntity->getIsDate()){
                if($this->imageCollectiveEntity->dateType == self::DATE_TYPE_WEEK){
                    $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 week')),date('Y-m-d',strtotime('1 days'))));
                }elseif($this->imageCollectiveEntity->dateType == self::DATE_TYPE_MONTH){
    			    $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 month')),date('Y-m-d')));
    			}elseif($this->imageCollectiveEntity->dateType == self::DATE_TYPE_THREEDAY){
    			    $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-3 days')),date('Y-m-d')));
    			}elseif($this->imageCollectiveEntity->dateType == self::DATE_TYPE_TWO_DAYS_AGO){
    				$imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_LESSEQUAL,date('Y-m-d',strtotime('-3 days')));
    			}    
            }
        }else{
            if($this->imageCollectiveEntity->getIsDate()){
                $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 month')),date('Y-m-d',strtotime('1 days'))));
            }
        }
        if($notIn){
            $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTIN, $notIn);
        }
        // isArticleTag = true 表示通过频道搜索 否则全站搜索
        if($this->imageCollectiveEntity->getIsArticleTag()){
            $imageCollTagFilter = new ImageCollTagFilter();
            $imageCollTagFilter->select(
                array(
                    ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,
                    ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,
                    ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,
                )
            );
            if(is_array($this->imageCollectiveEntity->getTagIds())){
                if($this->imageCollectiveEntity->isParentTag){
                    $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,Condition::CONDITION_IN,$this->imageCollectiveEntity->getTagIds());
                    $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY);
                }else{
                    $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_IN,$this->imageCollectiveEntity->getTagIds());
                }
            }else{
                if($this->imageCollectiveEntity->isParentTag){
                    $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,Condition::CONDITION_EQUAL,$this->imageCollectiveEntity->getTagIds());
                    $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY);
                }else{
                    $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_EQUAL,$this->imageCollectiveEntity->getTagIds());
                }
            }
            $imageCollFilter->leftJoin($imageCollTagFilter,null,ImageCollFilter::CONDITION_FIELD_NAME_ID,ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_TAG,false);
        } else {
            $imageCollTagFilter = new ImageCollTagFilter();
            $imageCollTagFilter->select(
                array(
                    ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,
                    ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,
                    ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,
                )
            );
            $imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY);
            $imageCollFilter->leftJoin($imageCollTagFilter,null,ImageCollFilter::CONDITION_FIELD_NAME_ID,ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_TAG,false);
        }
    
        
        if($this->imageCollectiveEntity->getOrderByNew()){
		    if($this->imageCollectiveEntity->orderType == self::SORT_DESC){
		       $imageCollFilter->order(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,false);
		    }elseif($this->imageCollectiveEntity->orderType == self::SORT_ASC){
		       $imageCollFilter->order(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,true);
		    }
		}
		
		if($this->imageCollectiveEntity->getOrderByClick()){
		    $imageCollPropertyFilter = new ImageCollPropertyFilter;
		    $imageCollPropertyFilter->select(
		        array(
		            ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
		            ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_EDITCLICK_NUM,
		        )
		    );
		    $imageCollFilter->order(ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_EDITCLICK_NUM,false,$imageCollPropertyFilter);
		    $imageCollFilter->leftJoin($imageCollPropertyFilter,null,ImageCollFilter::CONDITION_FIELD_NAME_ID,ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PROPERTY,false);
		}
    
    
        $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHED,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollective::STATUS_YES_PUBLISH);
    
    
        $bigLevel = $this->imageCollectiveEntity->getBigLevel();
        $level = $this->imageCollectiveEntity->getLevel();
        if(!empty($bigLevel)){
            if($bigLevel != $level){
                $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_GREATEREQUAL,$level);
                $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_LESSEQUAL,$bigLevel);
            }else {
                $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_EQUAL,$level);
            }
        }else {
            $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_GREATEREQUAL,$level);
        }
    
        $imageColls = $this->queryData->getAll($imageCollFilter);
        if($this->imageCollectiveEntity->getIsProImage()){
             
            foreach($imageColls as $coll){
                array_push($this->collIdArr,$coll->Id);
                $coll->SiteType = self::INFO_TYPE_IMAGE;
            }
            if($this->collIdArr){
                $this->collProImages = $this->getCollProImage($this->collIdArr,$this->imageCollectiveEntity->getProImageLevel());
            }
        }

        return $imageColls;
    }
    
    /**
     * 获取图集导读图
     * @param : Array $articleIdArr 文章ID数组
     * @param : Array $level 导读图等级
     *
     */
    public function getCollProImage($collIdArr,$proImageLevel){
        if(!is_array($proImageLevel)) $proImageLevel = (array)$proImageLevel;
        if(!is_array($collIdArr)) $collIdArr = (array)$collIdArr;
        $imageCollProFilter = new ImageCollProFilter();
        $imageCollProFilter->select(
            array(
                ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
                ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE,
                ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_ID,
            )
        );
        $imageLibraryFilter = new ImageLibraryFitler();
        $imageLibraryFilter->select(array(
            ImageLibraryFitler::CONDITION_FIELD_NAME_FILENAME,
        ));
        array_push($proImageLevel,\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO);
    
        $imageCollProFilter->where(ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE,Condition::CONDITION_IN,$proImageLevel);
        $imageCollProFilter->where(ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,Condition::CONDITION_IN,$collIdArr);
        $imageCollProFilter->leftJoin($imageLibraryFilter,null,ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_ID,ImageLibraryFitler::CONDITION_FIELD_NAME_ID,ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_FILENAME,false);
    
        $collProImages = $this->queryData->getAll($imageCollProFilter);
    
        $proImageArr = array();
        foreach($collProImages as $proImage){
            if(isset($proImageArr[$proImage->ImageCollectiveId])){
                $proImageArr[$proImage->ImageCollectiveId][$proImage->ImageType] = $proImage->FileName->Filename;
            }else{
                $proImageArr[$proImage->ImageCollectiveId] = array();
                $proImageArr[$proImage->ImageCollectiveId][$proImage->ImageType] = $proImage->FileName->Filename;
            }
        }
        return $proImageArr;
    }
    
    /**
     * 检验实体
     */
    public function checkEntity(){
        if($this->imageCollectiveEntity instanceof ImageCollectiveEntity){
            return true;
        }
        return false;
    }
    
    private function initVariable(){
        $this->articleIdArr = array();
        $this->collIdArr = array();
    
        $this->tagIdArr = array();
        $this->collTagIdArr = array();
    
        $this->articles = array();
        $this->imageColls = array();
    
        $this->articleProImages = array();
        $this->collProImages = array();
    
        $this->tagInfo = array();
        $this->collTagInfo = array();
    
        $this->articleTagArr = array();//文章关联标签数组
        $this->collTagArr = array();//图集关联标签数组
    }
    }